﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace OrderSystem
{
    public partial class FormUpdateOperator : FormTemplet
    {
        private int _nOperatorID = -1;

        private int _nRoleID = -1;

        public FormUpdateOperator()
        {
            this.InitializeComponent();
        }

        public FormUpdateOperator(int nOperatorID) : this()
        {
            this._nOperatorID = nOperatorID;
            DataTable dataTable = null;
            Operators operators = new Operators();
            try
            {
                dataTable = operators.LoadOperator(this._nOperatorID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            operators.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this.txtOperatorName.Text = dataTable.Rows[0]["用户姓名"].ToString();
                    if (Convert.ToBoolean(dataTable.Rows[0]["性别"]))
                    {
                        this.rbMale.Checked = true;
                    }
                    else
                    {
                        this.rbFemale.Checked = true;
                    }
                    this.txtPhone.Text = dataTable.Rows[0]["电话"].ToString();
                    this.txtAddress.Text = dataTable.Rows[0]["地址"].ToString();
                    this.txtEmail.Text = dataTable.Rows[0]["Email"].ToString();
                    this.txtLoginName.Text = dataTable.Rows[0]["登陆名"].ToString();
                    this.txtPassword.Text = dataTable.Rows[0]["密码"].ToString();
                    this._nRoleID = Convert.ToInt32(dataTable.Rows[0]["角色ID"]);
                    this.txtRole.Text = dataTable.Rows[0]["角色名称"].ToString() + "\r\n" + dataTable.Rows[0]["描述"].ToString();
                    this.txtRemark.Text = dataTable.Rows[0]["用户备注"].ToString();
                    this.btnOk.Enabled = true;
                }
                else
                {
                    MessageBox.Show("数据库里没有查到此 数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
        }

        public FormUpdateOperator(int operatorID, string operatorName, bool sex, string phone, string address, string email, string loginName, string password, int nRoleID, string remark) : this()
        {
            this._nOperatorID = operatorID;
            this.txtOperatorName.Text = operatorName;
            this.rbMale.Checked = sex;
            this.txtPhone.Text = phone;
            this.txtAddress.Text = address;
            this.txtEmail.Text = email;
            this.txtLoginName.Text = loginName;
            this.txtPassword.Text = password;
            this._nRoleID = nRoleID;
            this.txtRemark.Text = remark;
            this.btnOk.Enabled = true;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            RoleData roleData = FormSelectRole.ShowBox();
            if (roleData != null)
            {
                this._nRoleID = roleData.RoleID;
                this.txtRole.Text = roleData.RoleName + "\r\n" + roleData.Description;
                this.txtRemark.Focus();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.User_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.txtOperatorName.Text.Trim() == "" || this.txtOperatorName.Text.Trim().Length > 32)
            {
                MessageBox.Show("姓名不能为空或者不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtOperatorName.Focus();
            }
            else if (this.txtPhone.Text.Trim() == "" || this.txtPhone.Text.Trim().Length > 64)
            {
                MessageBox.Show("电话不能为空者不能大于64个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtPhone.Focus();
            }
            else if (this.txtAddress.Text.Trim() == "" || this.txtAddress.Text.Trim().Length > 128)
            {
                MessageBox.Show("地址不能为空者不能大于128个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtAddress.Focus();
            }
            else if (this.txtLoginName.Text.Trim() == "" || this.txtLoginName.Text.Trim().Length > 32)
            {
                MessageBox.Show("登陆名不能为空者不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLoginName.Focus();
            }
            else if (this.txtPassword.Text == "" || this.txtPassword.Text.Length > 32)
            {
                MessageBox.Show("密码不能为空者不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtPassword.Focus();
            }
            else
            {
                Operators operators = new Operators();
                bool flag = false;
                try
                {
                    flag = operators.UpdateOperator(this._nOperatorID, this.txtOperatorName.Text.Trim(), this.rbMale.Checked, this.txtPhone.Text.Trim(), this.txtAddress.Text.Trim(), this.txtEmail.Text.Trim(), this.txtLoginName.Text.Trim(), this.txtPassword.Text, this._nRoleID, this.txtRemark.Text.Trim());
                }
                catch (XmlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n配置文件出现错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (SqlException ex2)
                {
                    MessageBox.Show(ex2.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (IOException ex3)
                {
                    MessageBox.Show(ex3.Message + "\r\n\r\n配置文件丢失", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex4)
                {
                    MessageBox.Show(ex4.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                operators.Dispose();
                if (flag)
                {
                    MessageBox.Show("更改成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
